home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2011 April / ME_2011_04.iso / [Video-Tutorial] / 9430830 / directory.swf / scripts / __Packages / v2bScroller.as
Encoding:
Text File  |  2009-08-24  |  10.6 KB  |  375 lines

  1. class v2bScroller extends mx.core.UIComponent
  2. {
  3.    var onUnload;
  4.    var oSipDWs;
  5.    var view_area_width;
  6.    var view_area_height;
  7.    var target_mc;
  8.    var __width;
  9.    var __height;
  10.    var slider_mc;
  11.    var track_state_mc;
  12.    var read_target_size_delay;
  13.    var onMouseMove;
  14.    var mouseMoveListener = new Object();
  15.    var scroll_direction = "vertical";
  16.    var snap_offset = 0;
  17.    var scroll_percentage = 0;
  18.    var use_invisible_mask = false;
  19.    var auto_read_target_size = true;
  20.    var _enabled = false;
  21.    var read_target_size_interval = 60;
  22.    var target_start_x = 0;
  23.    var target_start_y = 0;
  24.    var target_height = 0;
  25.    var target_width = 0;
  26.    var scroller_height = 0;
  27.    var scroller_width = 0;
  28.    function v2bScroller()
  29.    {
  30.       super();
  31.       this.onUnload = function()
  32.       {
  33.          if(this.oSipDWs != undefined)
  34.          {
  35.             this.oSipDWs.removeMovieClip();
  36.             this.oSipDWs = undefined;
  37.          }
  38.       };
  39.    }
  40.    function set autoReadTargetSize(val)
  41.    {
  42.       this.auto_read_target_size = val;
  43.       this.invalidate();
  44.    }
  45.    function get autoReadTargetSize()
  46.    {
  47.       return this.auto_read_target_size;
  48.    }
  49.    function set useInvisibleMask(val)
  50.    {
  51.       this.use_invisible_mask = val;
  52.       this.invalidate();
  53.    }
  54.    function get useInvisibleMask()
  55.    {
  56.       return this.use_invisible_mask;
  57.    }
  58.    function set enabled(val)
  59.    {
  60.    }
  61.    function get enabled()
  62.    {
  63.       return this._enabled;
  64.    }
  65.    function set snapOffset(val)
  66.    {
  67.       this.snap_offset = val;
  68.       this.invalidate();
  69.    }
  70.    function get snapOffset()
  71.    {
  72.       return this.snap_offset;
  73.    }
  74.    function set scrollDirection(val)
  75.    {
  76.       this.scroll_direction = val;
  77.       this.invalidate();
  78.    }
  79.    function get scrollDirection()
  80.    {
  81.       return this.scroll_direction;
  82.    }
  83.    function set viewAreaWidth(val)
  84.    {
  85.       this.view_area_width = val;
  86.       this.invalidate();
  87.    }
  88.    function get viewAreaWidth()
  89.    {
  90.       return this.view_area_width;
  91.    }
  92.    function set viewAreaHeight(val)
  93.    {
  94.       this.view_area_height = val;
  95.       this.invalidate();
  96.    }
  97.    function get viewAreaHeight()
  98.    {
  99.       return this.view_area_height;
  100.    }
  101.    function set targetMovieClip(val)
  102.    {
  103.       var _loc3_ = String(this.target_mc);
  104.       if(typeof val == "string")
  105.       {
  106.          this.target_mc = this._parent[val];
  107.       }
  108.       else
  109.       {
  110.          this.target_mc = val;
  111.       }
  112.       if(this.oSipDWs != undefined)
  113.       {
  114.          this.oSipDWs.removeMovieClip();
  115.          this.oSipDWs = undefined;
  116.       }
  117.       this.target_start_x = Math.round(this.target_mc._x);
  118.       this.target_start_y = Math.round(this.target_mc._y);
  119.       this.target_mc._x = this.target_start_x;
  120.       this.target_mc._y = this.target_start_y;
  121.       this.invalidate();
  122.    }
  123.    function get targetMovieClip()
  124.    {
  125.       return this.target_mc;
  126.    }
  127.    function set scrollPercentage(val)
  128.    {
  129.       if(val < 0)
  130.       {
  131.          val = 0;
  132.       }
  133.       else if(val > 100)
  134.       {
  135.          val = 100;
  136.       }
  137.       this.scroll_percentage = val;
  138.       this.invalidate();
  139.    }
  140.    function get scrollPercentage()
  141.    {
  142.       return this.scroll_percentage;
  143.    }
  144.    function set targetHeight(val)
  145.    {
  146.       this.target_height = val;
  147.       this.invalidate();
  148.    }
  149.    function get targetHeight()
  150.    {
  151.       return this.target_height;
  152.    }
  153.    function set targetWidth(val)
  154.    {
  155.       this.target_width = val;
  156.       this.invalidate();
  157.    }
  158.    function get targetWidth()
  159.    {
  160.       return this.target_width;
  161.    }
  162.    function setSize(s_width, s_height)
  163.    {
  164.       if(s_width != null && s_width != undefined)
  165.       {
  166.          this.scroller_width = s_width;
  167.          this.__width = this.scroller_width;
  168.       }
  169.       if(s_height != null && s_height != undefined)
  170.       {
  171.          this.scroller_height = s_height;
  172.          this.__height = this.scroller_height;
  173.       }
  174.       this.invalidate();
  175.    }
  176.    function init()
  177.    {
  178.       super.init();
  179.    }
  180.    function createChildren()
  181.    {
  182.       var _loc2_ = this;
  183.       this.slider_mc.onPress = this.slider_onPress;
  184.       this.slider_mc.onRelease = this.slider_mc.onReleaseOutside = this.slider_onRelease;
  185.       this.slider_mc.onRollOver = this.slider_onRollOver;
  186.       this.slider_mc.onRollOut = this.slider_onRollOut;
  187.       this.size();
  188.    }
  189.    function draw()
  190.    {
  191.       super.draw();
  192.    }
  193.    function size()
  194.    {
  195.       super.size();
  196.       if(this.scroll_direction == "vertical")
  197.       {
  198.          this.slider_mc._rotation = 0;
  199.          this.slider_mc._y = 0;
  200.          this.track_state_mc._rotation = 0;
  201.          this.track_state_mc._y = 0;
  202.       }
  203.       else
  204.       {
  205.          this.slider_mc._rotation = -90;
  206.          this.slider_mc._y = this.slider_mc._height;
  207.          this.track_state_mc._rotation = -90;
  208.          this.track_state_mc._y = this.track_state_mc._height;
  209.       }
  210.       this.invalidate();
  211.    }
  212.    function invalidate()
  213.    {
  214.       super.invalidate();
  215.       var _loc3_ = this;
  216.       this.scroller_width = Math.round(this.__width);
  217.       this.scroller_height = Math.round(this.__height);
  218.       if(this.target_mc == undefined || this.target_mc._x == undefined || this.view_area_height == undefined || this.view_area_width == undefined)
  219.       {
  220.          if(this.oSipDWs != undefined)
  221.          {
  222.             this.oSipDWs.removeMovieClip();
  223.             this.oSipDWs = undefined;
  224.          }
  225.          this._visible = false;
  226.          this._enabled = false;
  227.          return undefined;
  228.       }
  229.       if(!this.use_invisible_mask)
  230.       {
  231.          if(this.oSipDWs == undefined)
  232.          {
  233.             this.addMask();
  234.             this.target_mc.setMask(this.oSipDWs);
  235.          }
  236.       }
  237.       else if(this.oSipDWs != undefined)
  238.       {
  239.          this.oSipDWs.removeMovieClip();
  240.          this.oSipDWs = undefined;
  241.       }
  242.       this.update();
  243.       if(this.auto_read_target_size)
  244.       {
  245.          this.read_target_size_delay = setInterval(_loc3_,"getTargetSize",this.read_target_size_interval);
  246.       }
  247.       else
  248.       {
  249.          clearInterval(this.read_target_size_delay);
  250.       }
  251.    }
  252.    function addMask()
  253.    {
  254.       var _loc2_ = "mask_" + Math.round(Math.random() * 10000000);
  255.       while(this._parent[_loc2_] != undefined)
  256.       {
  257.          _loc2_ = "mask_" + Math.round(Math.random() * 10000000);
  258.       }
  259.       this.oSipDWs = this.target_mc._parent.createEmptyMovieClip(_loc2_,this.target_mc._parent.getNextHighestDepth());
  260.       this.oSipDWs.beginFill(0);
  261.       this.oSipDWs.moveTo(0,0);
  262.       this.oSipDWs.lineTo(0,100);
  263.       this.oSipDWs.lineTo(100,100);
  264.       this.oSipDWs.lineTo(100,0);
  265.       this.oSipDWs.endFill();
  266.       this.oSipDWs._x = this.target_start_x;
  267.       this.oSipDWs._y = this.target_start_y;
  268.    }
  269.    function setMaskSize()
  270.    {
  271.       this.oSipDWs._width = this.view_area_width;
  272.       this.oSipDWs._height = this.view_area_height;
  273.    }
  274.    function update()
  275.    {
  276.       if(this.auto_read_target_size)
  277.       {
  278.          this.target_height = Math.ceil(this.target_mc._height);
  279.          this.target_width = Math.ceil(this.target_mc._width);
  280.       }
  281.       if(!this.use_invisible_mask)
  282.       {
  283.          this.setMaskSize();
  284.       }
  285.       if(this.scroll_direction == "vertical")
  286.       {
  287.          if(this.target_height + this.snap_offset < this.view_area_height)
  288.          {
  289.             this._visible = false;
  290.             this._enabled = false;
  291.             this.scroll_percentage = 0;
  292.          }
  293.          else
  294.          {
  295.             this._visible = true;
  296.             this._enabled = true;
  297.          }
  298.       }
  299.       else if(this.target_width + this.snap_offset < this.view_area_width)
  300.       {
  301.          this._visible = false;
  302.          this._enabled = false;
  303.          this.scroll_percentage = 0;
  304.       }
  305.       else
  306.       {
  307.          this._visible = true;
  308.          this._enabled = true;
  309.       }
  310.       if(this.scroll_direction == "vertical")
  311.       {
  312.          this.track_state_mc._height = this.scroller_height;
  313.          this.slider_mc._y = Math.round((this.scroller_height - this.slider_mc._height) * this.scroll_percentage / 100);
  314.          this.target_mc._y = Math.round(this.target_start_y - (this.target_height + this.snap_offset - this.view_area_height) * this.scroll_percentage / 100);
  315.       }
  316.       else
  317.       {
  318.          this.track_state_mc._rotation = 0;
  319.          this.track_state_mc._height = this.scroller_width;
  320.          this.track_state_mc._rotation = -90;
  321.          this.slider_mc._x = Math.round((this.scroller_width - this.slider_mc._width) * this.scroll_percentage / 100);
  322.          this.target_mc._x = Math.round(this.target_start_x - (this.target_width + this.snap_offset - this.view_area_width) * this.scroll_percentage / 100);
  323.       }
  324.    }
  325.    function slider_onPress()
  326.    {
  327.       this.gotoAndStop("pressed");
  328.       if(this._parent.scroll_direction == "vertical")
  329.       {
  330.          this.startDrag(false,0,0,0,this._parent.scroller_height - this._height);
  331.       }
  332.       else
  333.       {
  334.          this.startDrag(false,0,this._height,this._parent.scroller_width - this._width,this._height);
  335.       }
  336.       this.onMouseMove = function()
  337.       {
  338.          if(this._parent.scroll_direction == "vertical")
  339.          {
  340.             this._parent.scroll_percentage = this._parent.slider_mc._y * 100 / (this._parent.scroller_height - this._parent.slider_mc._height);
  341.             this._parent.target_mc._y = Math.round(this._parent.target_start_y - (this._parent.target_height + this._parent.snap_offset - this._parent.view_area_height) * this._parent.scroll_percentage / 100);
  342.          }
  343.          else
  344.          {
  345.             this._parent.scroll_percentage = this._parent.slider_mc._x * 100 / (this._parent.scroller_width - this._parent.slider_mc._width);
  346.             this._parent.target_mc._x = Math.round(this._parent.target_start_x - (this._parent.target_width + this._parent.snap_offset - this._parent.view_area_width) * this._parent.scroll_percentage / 100);
  347.          }
  348.       };
  349.    }
  350.    function slider_onRelease()
  351.    {
  352.       this.gotoAndStop("normal");
  353.       this.stopDrag();
  354.       this.onMouseMove();
  355.       delete this.onMouseMove;
  356.    }
  357.    function slider_onRollOver()
  358.    {
  359.       this.gotoAndStop("over");
  360.    }
  361.    function slider_onRollOut()
  362.    {
  363.       this.gotoAndStop("normal");
  364.    }
  365.    function getTargetSize()
  366.    {
  367.       if(this.target_height != Math.ceil(this.target_mc._height) || this.target_width != Math.ceil(this.target_mc._width))
  368.       {
  369.          this.target_height = Math.ceil(this.target_mc._height);
  370.          this.target_width = Math.ceil(this.target_mc._width);
  371.          this.update();
  372.       }
  373.    }
  374. }
  375.